package com.mfangsoft.zhuangjialong.app.seller.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mfangsoft.zhuangjialong.app.seller.model.Seller;
import com.mfangsoft.zhuangjialong.app.seller.service.ISellerService;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.CacheUtil;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.biotopeInfo.model.BiotopeInfo;

@Controller("appSellerController")
@RequestMapping("/app")
public class SellerController {

	@Autowired
	ISellerService sellerService;
	
	@RequestMapping(value="sellerLogin",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<Map<String,Object>> sellerLogin(@RequestBody Seller seller){
		ResponseMessage<Map<String, Object>> message;
		try {
			message = sellerService.sellerLogin(seller);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			ResponseMessage<Map<String,Object>> mess = new ResponseMessage<Map<String,Object>>();
			mess.setCode(SysConstant.FAILURE_CODE);
			mess.setMessage(SysConstant.FAILURE_MSG);
			return mess;
		}
	}
	@RequestMapping(value="selectStatistics",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<Map<String,Object>> selectStatistics(@RequestBody Map<String,Object> map){
		ResponseMessage<Map<String,Object>> message = new ResponseMessage<Map<String,Object>>();
		try {
			message.setResult(sellerService.selectStatistics(map.get("sellerId")==null?0:Integer.valueOf(map.get("sellerId").toString())));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	/**
	 * 查看今日邀请客户
	 * @param map
	 * @return
	 */
	@RequestMapping(value="selectTodayCustomer",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<Map<String,Object>> selectTodayCustomer(@RequestBody Map<String,Object> map){
		ResponseMessage<Map<String,Object>> message = new ResponseMessage<Map<String,Object>>();
		try {
			message.setResult(sellerService.selectTodayCustomer(map.get("sellerId")==null?0:Integer.valueOf(map.get("sellerId").toString())));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value="verifyCode",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<Boolean> verifyCode(@RequestBody Map<String,Object> map){
		ResponseMessage<Boolean> message = new ResponseMessage<Boolean>();
		String string;
		try {
			string = CacheUtil.Cache1.get("SMS" + map.get("phone").toString());
			if (map.get("vcode").toString().equals(string)) {
				message.setResult(true);
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				return message;
			}else{
				message.setResult(false);
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				return message;
			}
		} catch (ExecutionException e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	@RequestMapping(value="updateSellerPhone",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<String> updateSellerPhone(@RequestBody Map<String,Object> map){
		return sellerService.updateSellerPhone(map);
	}
	
	@RequestMapping(value="selectActivityInfo",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<Map<String,Object>> selectActivityInfo(@RequestBody Map<String,Object> map){
		ResponseMessage<Map<String,Object>> message = new ResponseMessage<Map<String,Object>>();
		try {
			message.setResult(sellerService.selectActivityInfo(map));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value="selectNode3Image",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<String> selectNode3Image(@RequestBody Map<String,Object> map){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			message.setResult(sellerService.selectNode3Image(map));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value="selectSellerPoint",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<List<Map<String,Object>>> selectSellerPoint(@RequestBody Map<String,Object> map){
		ResponseMessage<List<Map<String,Object>>> message = new ResponseMessage<List<Map<String,Object>>>();
		try {
			List<Map<String,Object>> sellerPoint = sellerService.selectSellerPoint(map.get("phone")+"");
			if(sellerPoint!=null && sellerPoint.size()>0){
				message.setResult(sellerPoint);
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
			}else{
				message.setCode("2");
				message.setMessage("未查询到积分信息");
			}
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	@RequestMapping(value="selectSellerInfo",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<List<Map<String,Object>>> selectSellerInfo(@RequestBody Map<String,Object> map){
		ResponseMessage<List<Map<String,Object>>> message = new ResponseMessage<List<Map<String,Object>>>();
		try {
			List<Map<String,Object>> sellerPoint = sellerService.selectSellerInfo(map.get("phone")+"");
			if(sellerPoint!=null && sellerPoint.size()>0){
				message.setResult(sellerPoint);
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
			}else{
				message.setCode("2");
				message.setMessage("该手机号未查询到导购信息");
			}
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	@RequestMapping(value="updateSellerPointStatus",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<String> updateSellerPointStatus(@RequestBody Map<String,Object> map){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			return sellerService.updateSellerPointStatus(map);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	/**
	 * 查看账号信息
	 */
	@RequestMapping(value="/selectAccountInformation/{user_id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<Map<String,Object>>> selectAccountInformation(@PathVariable Long user_id){
		ResponseMessage<List<Map<String,Object>>> message = new ResponseMessage<List<Map<String,Object>>>();
		try {
		    
			List<Map<String,Object>> result = sellerService.selectAccountInformation(user_id);
	
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(result);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;	
	}
	
	@RequestMapping(value="/selectSellerInfoById",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String,Object>> selectSellerInfoById(Integer sellerId){
		ResponseMessage<Map<String,Object>> message=new ResponseMessage<>();
		try {
			Map<String, Object> map = sellerService.selectSellerInfoById(sellerId);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(map);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	@RequestMapping(value="/updateBiotopeInfo",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> updateBiotopeInfo(@RequestBody BiotopeInfo biotopeInfo){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {	
			sellerService.updateBiotopeInfo(biotopeInfo);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;	
	}
	
	@RequestMapping(value="/selectBiotopeInfoPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Page<Map<String,Object>>> selectBiotopeInfoPage(@RequestBody Page<Map<String,Object>> page){
		ResponseMessage<Page<Map<String,Object>>> message = new ResponseMessage<Page<Map<String,Object>>>();
		try {
			message.setResult(sellerService.selectBiotopeInfoPage(page));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value="/selectCustomerInfoByPhone",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String,Object>> selectCustomerInfoByPhone(HttpServletRequest request){
		String phone = request.getParameter("phone");
		ResponseMessage<Map<String,Object>> message = new ResponseMessage<Map<String,Object>>();
		try {
			if(phone == null || "".equals(phone)){
				message.setMessage("手机号为空");
			}else{
				message.setResult(sellerService.selectCustomerInfoByPhone(phone));
				message.setMessage(SysConstant.SUCCESS_MSG);
			}
			message.setCode(SysConstant.SUCCESS_CODE);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	@RequestMapping(value="/selectCustomerInfoListBySellerPhone",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<Map<String, Object>>> selectCustomerInfoListBySellerPhone(HttpServletRequest request){
		String phone = request.getParameter("phone");
		ResponseMessage<List<Map<String, Object>>> message = new ResponseMessage<List<Map<String, Object>>>();
		try {
			if(phone == null || "".equals(phone)){
				message.setMessage("手机号为空");
			}else{
				message.setResult(sellerService.selectCustomerInfoListBySellerPhone(phone));
				message.setMessage(SysConstant.SUCCESS_MSG);
			}
			message.setCode(SysConstant.SUCCESS_CODE);
			
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
}
